package cn.tedu.myboot22.controller;

import cn.tedu.myboot22.mapper.EmpMapper;
import cn.tedu.myboot22.vo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class EmpController {

    @Autowired
    EmpMapper mapper;


    @RequestMapping("/select")
    @ResponseBody
    public String select() {
        List<Emp> list = mapper.select();

        //把员工的信息装进html标签中 最后返回给客户端
        String html = "<table border=1>";
        html += "<caption>员工列表</caption>";
        html += "<tr><th>id</th><th>名字</th><th>工作</th>" +
                "<th>年龄</th><th>工资</th><th>操作</th></tr>";
        for (Emp e : list) {
            html += "<tr>";
            html += "<td>" + e.getId() + "</td>";
            html += "<td>" + e.getName() + "</td>";
            html += "<td>" + e.getJob() + "</td>";
            html += "<td>" + e.getAge() + "</td>";
            html += "<td>" + e.getSalary() + "</td>";
            html += "<td><a href='/delete?id=" + e.getId() + "'>删除</a></td>";
            html += "</tr>";
        }
        html += "</table>";

        return html;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id) {
        mapper.delete(id);
        return "删除成功!<a href='/select'>员工列表</a>";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public String insert(Emp emp) {
        mapper.insert(emp);
        return "添加成功!<a href='/select'>员工列表</a>";
    }
}
